package com.gigalaxy.game.card.phom.utils
{
	public class Card
	{
		public function Card()
		{
		}
		
		public function getRank() {
			return rank;
		}
		
		public function getSuit(){
			return suit;
		}
		
		public function toString  {
			var rankStr;
			switch (this.getRank()) {
				case 1:
					rankStr = "A";
					break;
				case 11:
					rankStr = "J";
					break;
				case 12:
					rankStr = "Q";
					break;
				case 13:
					rankStr = "K";
					break;
				default:
					rankStr = this.getRank();
					break;
			}
			return rankStr + this.getSuit();
		}
		
		public static var SPADES_A = new Card(1, "♠");
		public static var SPADES_2 = new Card(2, "♠");
		public static var SPADES_3 = new Card(3, "♠");
		public static var SPADES_4 = new Card(4, "♠");
		public static var SPADES_5 = new Card(5, "♠");
		public static var SPADES_6 = new Card(6, "♠");
		public static var SPADES_7 = new Card(7, "♠");
		public static var SPADES_8 = new Card(8, "♠");
		public static var SPADES_9 = new Card(9, "♠");
		public static var SPADES_10 = new Card(10, "♠");
		public static var SPADES_J = new Card(11, "♠");
		public static var SPADES_Q = new Card(12, "♠");
		public static var SPADES_K = new Card(13, "♠");
		
		public static var CLUBS_A = new Card(1, "♣");
		public static var CLUBS_2 = new Card(2, "♣");
		public static var CLUBS_3 = new Card(3, "♣");
		public static var CLUBS_4 = new Card(4, "♣");
		public static var CLUBS_5 = new Card(5, "♣");
		public static var CLUBS_6 = new Card(6, "♣");
		public static var CLUBS_7 = new Card(7, "♣");
		public static var CLUBS_8 = new Card(8, "♣");
		public static var CLUBS_9 = new Card(9, "♣");
		public static var CLUBS_10 = new Card(10, "♣");
		public static var CLUBS_J = new Card(11, "♣");
		public static var CLUBS_Q = new Card(12, "♣");
		public static var CLUBS_K = new Card(13, "♣");
		
		public static var DIAMONDS_A = new Card(1, "♦");
		public static var DIAMONDS_2 = new Card(2, "♦");
		public static var DIAMONDS_3 = new Card(3, "♦");
		public static var DIAMONDS_4 = new Card(4, "♦");
		public static var DIAMONDS_5 = new Card(5, "♦");
		public static var DIAMONDS_6 = new Card(6, "♦");
		public static var DIAMONDS_7 = new Card(7, "♦");
		public static var DIAMONDS_8 = new Card(8, "♦");
		public static var DIAMONDS_9 = new Card(9, "♦");
		public static var DIAMONDS_10 = new Card(10, "♦");
		public static var DIAMONDS_J = new Card(11, "♦");
		public static var DIAMONDS_Q = new Card(12, "♦");
		public static var DIAMONDS_K = new Card(13, "♦");
		
		public static var HEARTS_A = new Card(1, "♥");
		public static var HEARTS_2 = new Card(2, "♥");
		public static var HEARTS_3 = new Card(3, "♥");
		public static var HEARTS_4 = new Card(4, "♥");
		public static var HEARTS_5 = new Card(5, "♥");
		public static var HEARTS_6 = new Card(6, "♥");
		public static var HEARTS_7 = new Card(7, "♥");
		public static var HEARTS_8 = new Card(8, "♥");
		public static var HEARTS_9 = new Card(9, "♥");
		public static var HEARTS_10 = new Card(10, "♥");
		public static var HEARTS_J = new Card(11, "♥");
		public static var HEARTS_Q = new Card(12, "♥");
		public static var HEARTS_K = new Card(13, "♥");
		
		public static var cardsBySuit = {
			"♠":[public static var SPADES_A, public static var SPADES_2, public static var SPADES_3, public static var SPADES_4, public static var SPADES_5, public static var SPADES_6, public static var SPADES_7, public static var SPADES_8, public static var SPADES_9, public static var SPADES_10, public static var SPADES_J, public static var SPADES_Q, public static var SPADES_K],
			"♣":[public static var CLUBS_A, public static var CLUBS_2, public static var CLUBS_3, public static var CLUBS_4, public static var CLUBS_5, public static var CLUBS_6, public static var CLUBS_7, public static var CLUBS_8, public static var CLUBS_9, public static var CLUBS_10, public static var CLUBS_J, public static var CLUBS_Q, public static var CLUBS_K],
			"♦":[public static var DIAMONDS_A, public static var DIAMONDS_2, public static var DIAMONDS_3, public static var DIAMONDS_4, public static var DIAMONDS_5, public static var DIAMONDS_6, public static var DIAMONDS_7, public static var DIAMONDS_8, public static var DIAMONDS_9, public static var DIAMONDS_10, public static var DIAMONDS_J, public static var DIAMONDS_Q, public static var DIAMONDS_K],
			"♥":[public static var HEARTS_A, public static var HEARTS_2, public static var HEARTS_3, public static var HEARTS_4, public static var HEARTS_5, public static var HEARTS_6, public static var HEARTS_7, public static var HEARTS_8, public static var HEARTS_9, public static var HEARTS_10, public static var HEARTS_J, public static var HEARTS_Q, public static var HEARTS_K]
		};
		public static var suits = ["♠", "♣", "♦", "♥"];
		public static var rankStrings = {
			"A":1,
			"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10,
			"J":11,
			"Q":12,
			"K":13
		};
		public static var cardOf = function(rank, suit) {
			if (public static var suits.indexOf(suit) == -1) {
				throw "Invalid suit " + suit;
			}
			if(rank < 1 || rank > 13) {
				throw "Invalid rank " + rank;
			}
			
			var cardsWithSuit = public static var cardsBySuit[suit];
			for(var i = 0; i < cardsWithSuit.length; i++) {
				if(cardsWithSuit[i].getRank() == rank) {
					return cardsWithSuit[i];
				}
			}
		};
		
		public static var RANK_COMPARATOR = function(card1, card2) {
			return card1.getRank() - card2.getRank();
		};
		
		
		public static var decodeString = function(str) {
			if (str === "") {
				return [];
			}
			
			var cardsStrs = str.trim().split(" ");
			var result = [];
			for(var i = 0; i < cardsStrs.length; i++) {
				var suitStr = cardsStrs[i].substring(cardsStrs[i].length - 1);
				var rankStr = cardsStrs[i].substring(0, cardsStrs[i].length - 1);
				result.push(public static var cardOf(public static var rankStrings[rankStr], suitStr));
			}
			return result;
		};
		public static var encodeString = function(cards) {
			var str = "";
			for(var i = 0; i < cards.length; i++) {
				str += cards[i].toString() + " ";
			}
			return str.trim();
		};
	}
}